import { RoleService } from './../../../service/role/role.service';
import { ResponseInterceptor } from './../../../interceptor/response/response.interceptor';
import { Controller, Get, UseInterceptors, Post, Body, Patch, Delete, Query } from '@nestjs/common';
import config from '../../../config/config';
import { RoleAccessService } from 'src/service/role_access/role_access.service';

@Controller(`${config.adminPath}/role`)
@UseInterceptors(ResponseInterceptor)
export class RoleController {
    constructor(private readonly roleService: RoleService, private readonly roleAccessService: RoleAccessService) {}
    @Get()
    async find() {
        return await this.roleService.find();
    }
    @Post()
    async create(@Body() body) {
        return await this.roleService.create(body);
    }
    @Patch()
    async update(@Body() body) {
        return await this.roleService.update(body);
    }
    @Delete()
    async delete(@Body() body) {
        return await this.roleService.delete(body);
    }
    @Get('auth')
    async getAuth(@Query() query) {
        const { role_id } = query;
        return await this.roleAccessService.find({ role_id });
    }
    @Post('auth')
    async auth(@Body() body) {
        const { role_id, access_ids = [] } = body;
        //先删后增
        await this.roleAccessService.deleteMany({ role_id });
        access_ids.forEach(async (access_id) => {
            await this.roleAccessService.create({
                role_id,
                access_id,
            });
        });
    }
}
